package cn.hznc.controller.service.toollife;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.controller.service.toolAnalyze.ToolLifeAnalyzeService;
import cn.hznc.controller.service.toollife.logic.ToolLifeDo;
import cn.hznc.controller.service.toollife.logic.ToolLifeLogic;
import cn.hznc.domain.entity.*;
import cn.hznc.domain.request.inventory.QueryAllToolPartReq;
import cn.hznc.domain.request.life.QryToolLifeConfigReq;
import cn.hznc.domain.request.life.QueryToolLifeReq;
import cn.hznc.domain.request.life.ToolUsageReportReq;
import cn.hznc.domain.request.toollife.EditOrAddToolLifeConfigReq;
import cn.hznc.domain.request.toollife.ToolLifeDataIndexReq;
import cn.hznc.domain.response.PageResult;
import cn.hznc.exception.*;
import cn.hznc.mapper.*;
import cn.hznc.repository.*;
import cn.hznc.utils.CommonUtil;
import cn.hznc.vo.*;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author：Administrator
 * @Date：2023/6/3 10:36
 */
@Service
@Slf4j
public class ToolLieService {

    @Resource
    private ToolLifeMapper toolLifeMapper;

    @Resource
    private ToolPartRespository toolPartRespository;

    @Resource
    private InventoryLocationRespository inventoryLocationRespository;

    @Resource
    private AssemblyToolInventoryRepository assemblyToolInventoryRepository;

    @Resource
    private TypeItemRepository typeItemRepository;

    @Resource
    private ToolLifeRepository toolLifeRepository;

    @Resource
    private ToolForkInfoRespository toolForkInfoRespository;

    @Resource
    private ToolForkInfoMapper toolForkInfoMapper;

    @Resource
    private MachineToolDataLogRepository machineToolDataLogRepository;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private ToolLifeAnalyzeService toolLifeAnalyzeService;

    @Resource
    private ToolPartMapper toolPartMapper;

    @Resource
    private ToolConnectMapper toolConnectMapper;

    @Resource
    private AssemblyToolInventoryMapper assemblyToolInventoryMapper;

    @Autowired
    private MinioProp minioProp;


    public Boolean editOrAddToolLifeConfig(EditOrAddToolLifeConfigReq editOrAddToolLifeConfigReq) {
        List<ToolLifeEntity> originToolLifeList = toolLifeMapper.selectList(new QueryWrapper<>());
        List<ToolLifeEntity> updateToolLifeList = editOrAddToolLifeConfigReq.getToolLifeReqList()
                .stream()
                .map(singleToolLifeReq -> {
                    ToolLifeEntity toolLifeEntity = new ToolLifeEntity();
                    BeanUtils.copyProperties(singleToolLifeReq, toolLifeEntity);
                    return toolLifeEntity;
                })
                .collect(Collectors.toList());
        ToolLifeLogic toolLifeLogic = new ToolLifeLogic(originToolLifeList, updateToolLifeList);
        toolLifeLogic.buildToolLifeDo();
        ToolLifeDo toolLifeDo = toolLifeLogic.getToolLifeDo();
        // 持久化
        persistCraft(toolLifeDo);
        return Boolean.TRUE;
    }

    private void persistCraft(ToolLifeDo toolLifeDo) {
        // 删除工艺
        List<Integer> deleteIds = toolLifeDo.getDeleteElement().stream().map(ToolLifeEntity::getId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(deleteIds)) {
            toolLifeMapper.deleteBatchIds(deleteIds);
        }
        // 新增工艺
        if (CollectionUtil.isNotEmpty(toolLifeDo.getCreateElement())) {
            toolLifeMapper.insertBatchSomeColumn(toolLifeDo.getCreateElement());
        }
        // 修改工艺
        if (CollectionUtil.isNotEmpty(toolLifeDo.getUpdateElement())) {
            toolLifeMapper.updateBatchById(toolLifeDo.getUpdateElement());
        }
    }

    public PageResult<ToolLifeIndexVo> queryToolLifeIndex(QueryToolLifeReq queryToolLifeReq) {
        if (StringUtils.isNotBlank(queryToolLifeReq.getWarehouseType())) {
            QueryAllToolPartReq queryAllToolPartReq = new QueryAllToolPartReq();
            queryAllToolPartReq.setWarehouseSearchTypes(queryToolLifeReq.getWarehouseType());
            List<Integer> locationIds = inventoryLocationRespository.selectLocationIds(queryAllToolPartReq);
            queryToolLifeReq.setLocationIds(locationIds);
        }
        PageHelper.startPage(queryToolLifeReq.getPageNum(), queryToolLifeReq.getPageSize());
        List<ToolLifeIndexVo> toolLifeIndexVos = toolPartRespository.queryToolLifeIndex(queryToolLifeReq);
        PageInfo<ToolLifeIndexVo> pageInfo = new PageInfo<>(toolLifeIndexVos);
        if (CollectionUtil.isNotEmpty(pageInfo.getList())) {
            pageInfo.getList().forEach(toolLifeIndexVo -> {
                String remainLife = toolLifeIndexVo.getRemainingLife();
                String life = toolLifeIndexVo.getLife();
                if (StringUtils.isNotEmpty(remainLife)) {
                    toolLifeIndexVo.setRemainingLife(remainLife.substring(0, remainLife.length() - 1));
                    toolLifeIndexVo.setLife(life.substring(0, life.length() - 1));
                }
                if (StringUtil.isNotEmpty(toolLifeIndexVo.getImage())) {
                    toolLifeIndexVo.setImage(minioProp.getBaseUrl() + minioProp.getBucketName() + "/" + toolLifeIndexVo.getImage());
                }
                String locationName = "";
                locationName = getLocationNameByLocationId(toolLifeIndexVo.getInventoryLocationId(), toolLifeIndexVo.getType());
                toolLifeIndexVo.setInventoryLocation(locationName);
                if (StringUtils.isNotEmpty(toolLifeIndexVo.getToolNo())) {
                    toolLifeIndexVo.setType(assemblyToolInventoryRepository.selectTypeByToolNo(toolLifeIndexVo.getToolNo()));
                } else {
                    toolLifeIndexVo.setType(typeItemRepository.getTypeNameByType(toolLifeIndexVo.getType()));
                }
            });
        }
        return new PageResult<>(pageInfo.getList(), pageInfo.getTotal());
    }

    private ToolLifeIndexVo setLocationNameAndType(ToolLifeIndexVo toolLifeIndexVo) {
        String remainLife = toolLifeIndexVo.getRemainingLife();
        String life = toolLifeIndexVo.getLife();
        if (StringUtils.isNotEmpty(remainLife)) {
            toolLifeIndexVo.setRemainingLife(remainLife.substring(0, remainLife.length() - 1));
            toolLifeIndexVo.setLife(life.substring(0, life.length() - 1));
        }
        if (StringUtil.isNotEmpty(toolLifeIndexVo.getImage())) {
            toolLifeIndexVo.setImage(minioProp.getBaseUrl() + minioProp.getBucketName() + "/" + toolLifeIndexVo.getImage());
        }
        String locationName = "";
        locationName = getLocationNameByLocationId(toolLifeIndexVo.getInventoryLocationId(), toolLifeIndexVo.getType());
        toolLifeIndexVo.setInventoryLocation(locationName);
        if (StringUtils.isNotEmpty(toolLifeIndexVo.getToolNo())) {
            toolLifeIndexVo.setType(assemblyToolInventoryRepository.selectTypeByToolNo(toolLifeIndexVo.getToolNo()));
        } else {
            toolLifeIndexVo.setType(typeItemRepository.getTypeNameByType(toolLifeIndexVo.getType()));
        }
        return toolLifeIndexVo;
    }

    public String getLocationNameByLocationId(Integer id, String type) {
        LocationNameVo locationNameVo = inventoryLocationRespository.getLocationNameById(id, type);
        if (ObjectUtil.isNotNull(locationNameVo)) {
            StringBuilder stringBuilder = new StringBuilder();
            return stringBuilder
                    .append(locationNameVo.getWarehouseName())
                    .append("-库位")
                    .append(locationNameVo.getLocationNum()).toString();
        }
        return "";
    }


    public Object qryToolLifeConfig(QryToolLifeConfigReq qryToolLifeConfigReq) {
        PageHelper.startPage(qryToolLifeConfigReq.getPageNum(), qryToolLifeConfigReq.getPageSize());
        PageInfo<ToolLifeEntity> pageInfo = new PageInfo<>();
        if (ObjectUtil.isNotNull(qryToolLifeConfigReq.getTypeFlag()) && qryToolLifeConfigReq.getTypeFlag() == 0) {
            pageInfo = new PageInfo<>(toolLifeRepository.selectToolList(qryToolLifeConfigReq));
        } else if (ObjectUtil.isNotNull(qryToolLifeConfigReq.getTypeFlag()) && qryToolLifeConfigReq.getTypeFlag() == 1) {
            pageInfo = new PageInfo<>(toolLifeRepository.selectAssemblyToolList(qryToolLifeConfigReq));
        }
        return new PageResult<>(pageInfo.getList(), pageInfo.getTotal());
    }

    public Object addToolLifeConfig(ToolLifeEntity toolLifeEntity) throws ToolLifeCycleException {
        if (ObjectUtil.isNotNull(toolLifeEntity.getId())) {
            //修改
            if (StringUtils.isNotBlank(toolLifeEntity.getClassify())) {
                QueryWrapper<ToolLifeEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("classify", toolLifeEntity.getClassify());
                List<ToolLifeEntity> toolLifeEntities = toolLifeMapper.selectList(wrapper);
                if (CollectionUtil.isNotEmpty(toolLifeEntities) && toolLifeEntities.size() > 1) {
                    throw new ToolLifeCycleException(ToolLifeCycleEx.CLASSIFY_REPEAT.getCode(), ToolLifeCycleEx.CLASSIFY_REPEAT.getMsg());
                }
            }
            getGroupIdnrReload(toolLifeEntity);
            toolLifeMapper.updateById(toolLifeEntity);
        } else {
            //新增
            QueryWrapper<ToolLifeEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("classify", toolLifeEntity.getClassify());
            wrapper.eq("type_flag", toolLifeEntity.getTypeFlag());
            ToolLifeEntity existEntity = toolLifeMapper.selectOne(wrapper);
            if (ObjectUtil.isNotNull(existEntity)) {
                throw new ToolLifeCycleException(ToolLifeCycleEx.CLASSIFY_REPEAT.getCode(), ToolLifeCycleEx.CLASSIFY_REPEAT.getMsg());
            }
            if (ObjectUtil.isNotNull(toolLifeEntity)) {
                if (toolLifeEntity.getTypeFlag() == 0) {
                    toolLifeEntity.setType(toolForkInfoRespository.selectTypeByPartno(toolLifeEntity.getClassify()));
                } else {
                    toolLifeEntity.setType(assemblyToolInventoryRepository.selectTypeByToolNo(toolLifeEntity.getClassify()));
                }
            }
            getGroupIdnrReload(toolLifeEntity);
            toolLifeMapper.insert(toolLifeEntity);
        }
        return Boolean.TRUE;
    }

    public Object deleteToolLifeConfig(Integer id) {
        if (ObjectUtil.isNotNull(id)) {
            toolLifeMapper.deleteById(id);
        }
        return Boolean.TRUE;
    }

    public String qryToolLifeType(String type) {
        return typeItemRepository.selectTypeNameByType(type);
    }

    public Object qryToolLifeUsageHistory(String uniqueId, Integer pageNum, Integer pageSize) {
//        PageHelper.startPage(pageNum , pageSize);
//        QueryWrapper<MachineToolDataLog> wrapper = new QueryWrapper<>();
//        wrapper.eq("unique_id" , uniqueId);
//        PageInfo<MachineToolDataLog> pageInfo = new PageInfo<>(machineToolDataLogMapper.selectList(wrapper));
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<MachineToolDataLog> pageInfo = new PageInfo<>(machineToolDataLogRepository.selectDataLog(uniqueId));
        return new PageResult<>(pageInfo.getList(), pageInfo.getTotal());
    }

    public HashMap<String, Object> toolUsageReport(ToolUsageReportReq toolUsageReportReq) throws ToolScheduleException {
        QueryWrapper<OrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("task_id", toolUsageReportReq.getTaskId());
        OrderEntity orderEntity = orderMapper.selectOne(wrapper);
        List<ToolReportVo> resultList = new ArrayList<>();
        List<ToolReportVo> pageList = new ArrayList<>();
        if (ObjectUtil.isNull(orderEntity)) {
            throw new ToolScheduleException(ToolManageEx.ORDER_NO_EXIST.getCode(), ToolManageEx.ORDER_NO_EXIST.getMsg());
        }
        //PageHelper.startPage(toolUsageReportReq.getPageNum() , toolUsageReportReq.getPageSize());
        //List<ToolReportVo> toolReportVos = machineToolDataLogRepository.selectToolReportByTaskId(toolUsageReportReq.getTaskId() , toolUsageReportReq.getId());
        //PageInfo<ToolReportVo> pageInfo = new PageInfo<>(toolReportVos);
        resultList = machineToolDataLogRepository.selectToolReportByTaskId(toolUsageReportReq.getTaskId(), toolUsageReportReq.getId());
        //resultList = resultList.stream().filter(toolReportVo -> StringUtils.isNotBlank(toolReportVo.getToolNo()) &&toolReportVo.getToolNo().equals("T9129301")).collect(Collectors.toList());
        resultList.forEach(toolReportVo -> {
            toolReportVo.setOrderEntity(orderEntity);
            //设置刀具号 刀片唯一标识  刀柄唯一标识
            ToolPartEntity toolPartEntity = getUniqueToolPart(toolReportVo.getUniqueId());
            if (ObjectUtil.isNotNull(toolPartEntity)) {
                toolReportVo.setToolNo(toolPartEntity.getToolNo());
                //String params = toolPartEntity.getParams();
                String params = toolReportVo.getToolParams();
                if (StringUtil.isNotEmpty(params)) {
                    JSONObject jsonObject = JSONObject.parseObject(params);
                    List<String> toolTypes = (List<String>) jsonObject.get("toolType");
                    toolReportVo.setToolUniqueId(toolTypes.get(0));
                    //获得刀片金额
                    ToolPartEntity uniqueToolPart = getUniqueToolPart(toolTypes.get(0));
                    BigDecimal price = toolForkInfoRespository.selectPriceByPartno(uniqueToolPart.getPartno());
                    if (ObjectUtil.isNotNull(price) && ObjectUtil.isNotNull(toolReportVo.getToolLife()) && toolReportVo.getToolLife() > 0.0) {
                        toolReportVo.setTime(ObjectUtil.isNotNull(toolReportVo.getTime()) ? toolReportVo.getTime() : 0);
                        if (toolReportVo.getTime() == 0) {
                            toolReportVo.setCost(0.0);
                        } else {
                            toolReportVo.setUnitPrice(price);
                            toolReportVo.setCost((new BigDecimal(toolReportVo.getTime()).divide(new BigDecimal(toolReportVo.getToolLife()), 3, BigDecimal.ROUND_CEILING).multiply(price)).doubleValue());
                        }
                    }
                    List<String> shankTypes = (List<String>) jsonObject.get("shankType");
                    toolReportVo.setShankUniqueId(shankTypes.get(0));
                } else {
                    //参数为空情况下采用型号作为查询条件
                    Map<String, String> partTypeByToolNo = getPartTypeByToolNo(toolReportVo.getToolNo());
                    String toolType = partTypeByToolNo.get("toolType");
                    Double price = getPrice(toolType);
                    toolReportVo.setUnitPrice(new BigDecimal(price));
                    toolReportVo.setCost((new BigDecimal(toolReportVo.getTime()).divide(new BigDecimal(toolReportVo.getToolLife()), 3, BigDecimal.ROUND_CEILING).multiply(new BigDecimal(price))).doubleValue());
                }
            }
        });
        //分页数据对于找不到sn刀具的采用部件类型
        if (CollectionUtil.isNotEmpty(resultList)) {
            resultList.forEach(toolReportVo -> {
                if (StringUtil.isEmpty(toolReportVo.getToolUniqueId())) {
                    Map<String, String> partTypeByToolNo = getPartTypeByToolNo(toolReportVo.getToolNo());
                    toolReportVo.setToolUniqueId(partTypeByToolNo.get("toolType"));
                    toolReportVo.setToolUniqueId(partTypeByToolNo.get("shankType"));
                }
            });
        }
        if (CollectionUtil.isNotEmpty(resultList)) {
            orderEntity.setTotalCost(resultList
                    .stream()
                    .filter(toolReportVo -> ObjectUtil.isNotNull(toolReportVo.getCost()))
                    .mapToDouble(ToolReportVo::getCost).sum());
            orderEntity.setTotalTime(resultList
                    .stream()
                    .filter(toolReportVo -> ObjectUtil.isNotNull(toolReportVo.getTime()))
                    .mapToDouble(ToolReportVo::getTime).sum());
        }
        if (CollectionUtil.isNotEmpty(resultList)) {
            pageList = CommonUtil.pageList(resultList, toolUsageReportReq.getPageNum(), toolUsageReportReq.getPageSize());

        }
        HashMap<String, Object> retMap = new HashMap<>();
        retMap.put("orderEntity", orderEntity);
        retMap.put("toolReportVos", new PageResult<>(pageList, (long) resultList.size()));
        return retMap;
    }

    private Double getPrice(String toolType) {
        QueryWrapper<ToolForkInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("idnr", toolType);
        List<ToolForkInfoEntity> toolForkInfoEntities = toolForkInfoMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(toolForkInfoEntities)) {
            List<Double> priceList = toolForkInfoEntities
                    .stream()
                    .filter(toolForkInfo -> ObjectUtil.isNotNull(toolForkInfo.getPrice()))
                    .map(ToolForkInfoEntity::getPrice)
                    .collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(priceList)) {
                return CollectionUtil.max(priceList);
            }
        }
        return 0.0;
    }

    private Map<String, String> getPartTypeByToolNo(String toolNo) {
        Map<String, String> retMap = new HashMap<>();
        QueryWrapper<ToolConnectEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("tool_no", toolNo);
        List<ToolConnectEntity> toolConnectEntities = toolConnectMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(toolConnectEntities)) {
            retMap.put("toolType", toolConnectEntities.get(0).getPartType());
        }
        QueryWrapper<AssemblyToolInventoryEntity> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("tool_no", toolNo);
        List<AssemblyToolInventoryEntity> assemblyToolInventoryEntities = assemblyToolInventoryMapper.selectList(wrapper1);
        if (CollectionUtil.isNotEmpty(assemblyToolInventoryEntities)) {
            if (ObjectUtil.isNotNull(retMap.get("toolType"))) {
                retMap.put("toolType", assemblyToolInventoryEntities.get(0).getToolType());
            }
            retMap.put("shankType", assemblyToolInventoryEntities.get(0).getShankType());
        }
        return retMap;
    }

    private ToolPartEntity getUniqueToolPart(String uniqueId) {
        QueryWrapper<ToolPartEntity> toolPartWrapper = new QueryWrapper<>();
        toolPartWrapper.eq("unique_id", uniqueId);
        ToolPartEntity toolPartEntity = toolPartMapper.selectOne(toolPartWrapper);
        return toolPartEntity;
    }

    public Object qryToolLifeUsageHistoryList(String uniqueId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<MachineToolDataLog> pageInfo = new PageInfo<>(machineToolDataLogRepository.selectDataLogList(uniqueId));
        return new PageResult<>(pageInfo.getList(), pageInfo.getTotal());
    }

    public Object toolLifeDataIndex(ToolLifeDataIndexReq toolLifeDataIndexReq) {
        List<ToolLifeDataIndexVo> toolLifeDataIndexVos = machineToolDataLogRepository.qryToolLifeDataIndex(toolLifeDataIndexReq.getToolNo());
        List<ToolLifeDataIndexResultVo> resultList = new ArrayList<>();
        List<ToolLifeDataIndexResultVo> pageList = new ArrayList<>();
        //根据toolNo分组
        if (CollectionUtil.isNotEmpty(toolLifeDataIndexVos)) {
            Map<String, List<ToolLifeDataIndexVo>> groupByToolNoMap = toolLifeDataIndexVos.stream().collect(Collectors.groupingBy(ToolLifeDataIndexVo::getToolNo));
            groupByToolNoMap.forEach((k, v) -> {
                ToolLifeDataIndexResultVo toolLifeDataIndexResultVo = new ToolLifeDataIndexResultVo();
                toolLifeDataIndexResultVo.setToolNo(k);
                toolLifeDataIndexResultVo.setToolLifeDataIndexVos(v);
                //计算数学期望
                toolLifeDataIndexResultVo.setExpectedValue(toolLifeAnalyzeService.calculateExpectedValue(v.stream().map(ToolLifeDataIndexVo::getMaxToolTimeUsage).collect(Collectors.toList())));
                //取最小额定寿命
                List<ToolLifeDataIndexVo> ascToolLifeList = v.stream().sorted(Comparator.comparing(ToolLifeDataIndexVo::getToolLife)).collect(Collectors.toList());
                toolLifeDataIndexResultVo.setMinToolLife(ascToolLifeList.get(0).getToolLife());
                //取最小使用寿命
                List<ToolLifeDataIndexVo> ascTimeUsageLifeList = v.stream().sorted(Comparator.comparing(ToolLifeDataIndexVo::getMaxToolTimeUsage)).collect(Collectors.toList());
                toolLifeDataIndexResultVo.setMinToolTimeUsage(ascTimeUsageLifeList.get(0).getMaxToolTimeUsage());
                resultList.add(toolLifeDataIndexResultVo);
            });
        }
        //手动分页
        pageList = CommonUtil.pageList(resultList, toolLifeDataIndexReq.getPageNum(), toolLifeDataIndexReq.getPageSize());
        if (CollectionUtil.isNotEmpty(pageList)) {
            pageList.forEach(toolLifeDataIndexResultVo -> toolLifeDataIndexResultVo.setTypeName(ObjectUtil.isNotNull(assemblyToolInventoryRepository.selectTypeByToolNo(toolLifeDataIndexResultVo.getToolNo())) ? assemblyToolInventoryRepository.selectTypeByToolNo(toolLifeDataIndexResultVo.getToolNo()) : "铣削刀具"));
        }
        return new PageResult<>(pageList, (long) resultList.size());
    }

    public Object qryToolPartUsageHistoryList(String uniqueId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<MachineToolDataLog> pageInfo = new PageInfo<>(machineToolDataLogRepository.selectDataLogListReload(uniqueId));
        return new PageResult<>(pageInfo.getList(), pageInfo.getTotal());
    }

    public Object toolLifeData(String toolNo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ToolLifeDataIndexVo> toolLifeDataIndexVos = machineToolDataLogRepository.qryToolLifeDataIndex(toolNo);
        PageInfo<ToolLifeDataIndexVo> pageInfo = new PageInfo<>(toolLifeDataIndexVos);
        return new PageResult<>(pageInfo.getList(), pageInfo.getTotal());
    }

    public Object getByUniqueId(String uniqueId) {
        QueryWrapper<ToolPartEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("unique_id", uniqueId);
        return toolPartMapper.selectOne(wrapper);
    }

    public Object getGroupIdnr() {
        List<ToolLifeEntity> updateList = new ArrayList<>();
        QueryWrapper<ToolLifeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("type", 0);
        List<ToolLifeEntity> toolLifeEntities = toolLifeMapper.selectList(wrapper);
        for (ToolLifeEntity toolLifeEntity : toolLifeEntities) {
            if (CollectionUtil.isNotEmpty(toolForkInfoRespository.selectPartTypeByPartnoReload(toolLifeEntity.getClassify()))) {
                toolLifeEntity.setGroupIdnr(toolForkInfoRespository.selectPartTypeByPartnoReload(toolLifeEntity.getClassify()).get(0));
                updateList.add(toolLifeEntity);
            }
        }
        if (CollectionUtil.isNotEmpty(updateList)) {
            toolLifeMapper.updateBatchById(updateList);
        }
        return Boolean.TRUE;
    }

    public ToolLifeEntity getGroupIdnrReload(ToolLifeEntity toolLifeEntity) {
        if (CollectionUtil.isNotEmpty(toolForkInfoRespository.selectPartTypeByPartnoReload(toolLifeEntity.getClassify()))) {
            toolLifeEntity.setGroupIdnr(toolForkInfoRespository.selectPartTypeByPartnoReload(toolLifeEntity.getClassify()).get(0));
            return toolLifeEntity;
        }
        return toolLifeEntity;
    }
}
